import Vue from 'vue'
import Vuex from 'vuex'
import router, {resetRouter} from '../router'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    currentMenu: null,
    currentUser: '等待中...',    //默认初始未登录用户名
    isLogin: '',     //初始时候给一个 isLogin=0 表示用户未登录
  },
  //vuex规定所有state里数据必须通过mutation方法来修改
  mutations: {
    selectMenu (state, val){
      if (val.name === '首页'){
        state.currentMenu = null
      }else {
        state.currentMenu = val
      }
    },
    getUser(state){
      state.currentUser = localStorage.getItem("username")
    },
    getPermission(state, data){     //前台权限校验
      state.isLogin = data;
    },
    logout(){
      router.push('/login')
      localStorage.removeItem("username")

      //退出重置路由
      resetRouter()
    }
  },
  actions: {
  },
  modules: {
  },
  plugins: [
      createPersistedState
  ]
})
